-- buff
-- create by panyl
-- 技能带来一条附加属性
--     buff(status,value,round)：第一个参数为状态名称、第二个是属性值，第三个参数为回合数（如果没有默认为一直起效）
-- 附加属性都记录在目标的prop/buff字段下，战斗开始或结束时将被删除，每回合开始时处理一遍(过期的删掉)


return {
    apply = function(source, target, skillId, round, para, extra)
        local effectRound = - 1;

        extra = extra or {};
        local lastRound = extra.lastRound;

        if type(lastRound) == "number" then
            effectRound = round + lastRound + 1;
        elseif para[3] > 0 then
            -- 不包括本回合
            effectRound = round + para[3] + 1;
        end

        local prop = PropM.combine(source, "magic_last", skillId);
        effectRound = PropM.apply(prop, effectRound);
        prop = PropM.combine(source, "magic_last", "assist");
        effectRound = PropM.apply(prop, effectRound);

        -- 所有技能持续时间+
        prop = PropM.combine(source, "magic_last", "all");
        effectRound = PropM.apply(prop, effectRound);

        local v = para[2];

        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, true);

        --最终威力
        local v = math.modf(v * (addon_rate + 1000) / 1000);

        -- 攻击方的魔力
        local magic = source:getMagic();
        -- 如果是静电场，需要去掉静电场增加的额外魔力
        if para[1] == "elect" then
            prop = PropM.combine(source, "attrib_elect", "magic");
            magic = magic - prop[3];
        end

        v = math.max(1, math.modf(v * magic / 1000));

        local condition = {
            ["value"] = v,
            ["end_round"] = effectRound,
        };

        -- 增加命中动作
        SkillM.getSequence(target):hit(source, target, skillId);

        -- 天赋起效，静电场同时提升攻击
        prop = PropM.combine(source, "elect_add_attack", skillId);
        if PropM.apply(prop, 1) > 0 then
            condition["attack"] = v;
        end

        -- 附加状态
        CombatStatusM.applyStatus(target, para[1], condition);

        -- 作用目标
        return { target, };
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { arr[1], tonumber(arr[2]), tonumber(arr[3] or -1), };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        -- 效果
        local vbase = para[2];

        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, false);

        --最终威力
        local vforce = math.modf(vbase * (addon_rate + 1000)/1000);

        -- 攻击方的魔力
        local magic = source:getMagic();
        -- 如果是静电场，需要去掉静电场增加的额外魔力
        local prop;
        if para[1] == "elect" then
            prop = PropM.combine(source, "attrib_elect", "magic");
            magic = magic - prop[3];
        end

        local addForce = math.max(1, math.modf(vforce * magic / 1000));
        local addBase = math.max(1, math.modf(vbase * magic / 1000));
        local addAddon = addForce - addBase;
        if addAddon == 0 then
            desc = string.gsub(desc, "{buff}", addForce);
        else
            desc = string.gsub(desc, "{buff}", addBase .. "(+" .. addAddon .. ")");
        end

        -- 回合数的显示
        local baseRound = para[3];
        local round = para[3];
        -- 增加持续时间的效果
        prop = PropM.combine(source, "magic_last", skillId);
        round = PropM.apply(prop, round);
        prop = PropM.combine(source, "magic_last", "assist");
        round = PropM.apply(prop, round);
        prop = PropM.combine(source, "magic_last", "all");
        round = PropM.apply(prop, round);

        local addonRound = round - baseRound;
        if addonRound == 0 then
            desc = string.gsub(desc, "{round}", baseRound);
        else
            desc = string.gsub(desc, "{round}", baseRound .. "(+" .. addonRound .. ")");
        end

        return desc;
    end,
};
